package refection;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class RefectDemo {
    public static void reflectNewInstance(){
        Class<?> c1;
        //如何通过反射实例化对象
        try {
           c1 =  Class.forName("refection.Student");
           Student  s1 = (Student) c1.newInstance();
            System.out.println(s1);
        }
             catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
            catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    //反射私有构造方法      屏蔽内容为获得公有的构造方法
    private static void reflectPrivateConstructor(){
        Class<?> c1;
        //如何通过反射实例化对象
        try {
            c1 =  Class.forName("refection.Student");
           Constructor<Student> con = (Constructor<Student>) c1.getDeclaredConstructor(String.class,int.class);
            //获取权限设为true
            con.setAccessible(true);
           Student student = con.newInstance("zhangsan",18);
            System.out.println(student);
        }
        catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
//反射私有属性
    public static void reflectPrivateField(){
        Class<?> c1;
        try {
            c1 = Class.forName("refection.Student");
           Field field = c1.getDeclaredField("name");
           field.setAccessible(true);
           Student student = (Student)c1.newInstance();
           field.set(student,"wangwu");
            System.out.println(student);
        } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    //反射私有方法
    public static void reflectPrivateMethod(){
        Class<?> c1;
        try {
            c1 = Class.forName("refection.Student");
            Method method = c1.getDeclaredMethod("function", String.class);
            method.setAccessible(true);
            Student student = (Student) c1.newInstance();
            method.invoke(student,"我是一个参数");
            } catch (InstantiationException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
         catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        reflectNewInstance();
        reflectPrivateConstructor();
        reflectPrivateField();
    }
}

